from django.shortcuts import render
from django.contrib.auth.models import Group
from rest_framework import mixins
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework import permissions
from rest_framework import serializers
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from guardian.shortcuts import assign_perm, get_perms
from .serializers import UserWantMovieSerializer, UserStarMovieReadSerializer, UserStarMovieCreateSerializer, \
    UserStarMovieUpdateSerializer, UserMovieShortCommentsSerializer, UserMovieShortUpdateCommentsSerializer, \
    UserMovieShortCommentsReadSerializer
from .models import UserStarMovie

from .throttles import UserMovieShortCommentsThrottle


# Create your views here.

class UserWantMovieViewSet(mixins.CreateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin,
                           mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    """
    list:
        用户去个人中心查看自己对这个电影的想看
    retrieve:
        用户去个人中心查看自己对这个电影的想看
    create:
        用户去点击想看一个电影
    destroy:
        用户取消想看一个电影
    """
    serializer_class = UserWantMovieSerializer
    # 总体来约束你这个接口
    queryset = serializer_class.Meta.model.objects.all()
    authentication_classes = (JSONWebTokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)

    def get_queryset(self):
        user = self.request._user
        return self.get_serializer().Meta.model.objects.filter(user=user)


class UserStarMovieViewSet(mixins.CreateModelMixin, mixins.UpdateModelMixin, mixins.ListModelMixin,
                           mixins.RetrieveModelMixin, viewsets.GenericViewSet):
    """
    list:
        用户去个人中心查看自己对这个电影的评分
    retrieve:
        用户去个人中心查看自己对这个电影的评分
    create:
        用户给一个电影评分
    update:
        用户修改评分
    """
    serializer_class = UserStarMovieReadSerializer
    queryset = serializer_class.Meta.model.objects.all()
    authentication_classes = (JSONWebTokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)

    def get_queryset(self):
        return self.get_serializer().Meta.model.objects.filter(user=self.request.user)

    def get_serializer_class(self):
        if self.action == 'create':
            return UserStarMovieCreateSerializer
        if self.action in ("list", 'retrieve'):
            return UserStarMovieReadSerializer
        if self.action in ('update', 'partial_update'):
            return UserStarMovieUpdateSerializer


"""

评论的查看：
    1.你在电影下面去查看所有的评论
    2.你在你的用户中心去查看你的评论
"""


class UserMovieShortCommentsViewSet(viewsets.ModelViewSet):
    """
    create:
        用户去添加一条短评

    update:
        用户去修改他的短评(修改他的内容,分数)

    partial_update:
        用户去修改他的短评(修改他的内容,分数)

    destroy:
        用户去删除他的短评

    list:
        你在你的用户中心去查看你的评论

    retrieve:
        你在你的用户中心去查看你的评论
    """

    serializer_class = UserMovieShortCommentsSerializer
    queryset = serializer_class.Meta.model.objects.all()
    authentication_classes = (JSONWebTokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)
    throttle_classes = (UserMovieShortCommentsThrottle,)

    def get_queryset(self):
        if self.action in ('retrieve', 'list'):
            return self.get_serializer_class().Meta.model.objects.filter(user=self.request.user)

    def get_serializer_class(self):
        if self.action in ("update", "partial_update"):
            return UserMovieShortUpdateCommentsSerializer

        return UserMovieShortCommentsSerializer

    def perform_create(self, serializer):
        obj = serializer.save()
        assign_perm('can_delete_shortcomments', self.request.user, obj)
        assign_perm('can_update_shortcomments', self.request.user, obj)
        assign_perm('can_read_shortcomments', self.request.user, obj)
        group = Group.objects.get_or_create(name='site_managers')[0]
        assign_perm('can_delete_shortcomments', group, obj)

    def perform_destroy(self, instance):

        permissions = get_perms(self.request._user, instance)
        if 'can_delete_shortcomments' not in permissions:
            raise serializers.ValidationError('你没有权限去删除')

        super().perform_destroy(instance)


class UserMovieShortCommentsReadViewSet(viewsets.ReadOnlyModelViewSet):
    """
    list:
        查看所有短评

    retrieve:
        查看一条短评

    """
    serializer_class = UserMovieShortCommentsReadSerializer
    queryset = serializer_class.Meta.model.objects.all()


"""
1.用户可以发帖(在不被封号的情况下) -> 用户有发帖的权限 
2.用户可以修改或者删除自己的帖子 —> 用户修改查看删除帖子的权限
3.管理员(组)也可以删除你的帖子（不能修改）->管理员有删除帖子的权限
4.其他普通用户也只能看我们的帖子,不能删除和修改 ->其他用户只有查看帖子的权限
5.管理员可以去除掉你删除或者修改帖子权限 -> 去除用户删除帖子的权限
"""

"""
1.自己可以发帖(没有封号的功能)
2.只能是自己去修改或者删除自己的帖子
"""

"""
OA系统:
    会计 查看所有人的工资 model
    讲师 查看自己的工资 obj

"""

"""
type = ''爱情''

if type:

    select * from movie_detail where type='爱情' and movie_state='大陆' order_by = 'id'
    
else :
    
"""
